/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package persistence;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import vo.rb_emp_salary_structure;

/**
 *
 * @author Shuhail
 */
public class rb_emp_sal_structure_persist {
    
    Session sess = null;
    Transaction tx = null;
    
    private static rb_emp_sal_structure_persist getInstance() {
        
        return new rb_emp_sal_structure_persist();
    }
    
    public rb_emp_sal_structure_persist() {
        
        SessionFactory sf = new Configuration().configure().buildSessionFactory();
        sess = sf.openSession();
        tx = sess.beginTransaction();
    }
    
    public List getAllEmployeeSAlary() {
        List list = null;
        Query query = sess.getNamedQuery("getAllEmployeeSalary");
        list = query.list();
        return list;
    }
    
    public boolean updateAllEmployeeSalary(List list) {
       
        tx.begin();
        Iterator it =list.iterator();
        while(it.hasNext()){
            
        rb_emp_salary_structure emp = (rb_emp_salary_structure)it.next();
         
        rb_emp_salary_structure empVO = (rb_emp_salary_structure)sess.get(rb_emp_salary_structure.class, emp.getEmployee_id());
        System.out.println(empVO);
        if(empVO==null){
           sess.save(emp);
        }
        else {
            System.out.println("Updating");
            empVO.setBasic_pay(emp.getBasic_pay());
            empVO.setDa(emp.getDa());
            empVO.setNet_salary(emp.getNet_salary());
            empVO.setPhone(emp.getPhone());
            empVO.setTa(emp.getTa());
          sess.update(empVO);  
        }
        }
        tx.commit();
        return true;
    }
    public boolean insertEmployeeSalary(rb_emp_salary_structure emp) {
        
        tx.begin();
        sess.save(emp);
        tx.commit();
        return true;
    }
    
    public static void main(String[] args) {
        rb_emp_salary_structure emp = new rb_emp_salary_structure();
        emp.setEmployee_id("E3");
        emp.setBasic_pay(123411);
        emp.setDa(111);
        emp.setNet_salary(11111);
        emp.setPhone("1222AAA4");
        emp.setTa(11111);
        List<rb_emp_salary_structure> list = new ArrayList<rb_emp_salary_structure>();
        list.add(emp);
        rb_emp_sal_structure_persist.getInstance().updateAllEmployeeSalary(list);
        
    }
   
}
